package kz.pompei.ifacer.dialogs;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

public class TextAreaDialog {
  public static void main(String[] args) {
    final JFrame f = new JFrame("fdfdsfd");
    f.setLocation(100, 100);
    f.setSize(500, 300);
    
    f.addMouseListener(new MouseListener() {
      @Override
      public void mouseReleased(MouseEvent e) {}
      
      @Override
      public void mousePressed(MouseEvent e) {}
      
      @Override
      public void mouseExited(MouseEvent e) {}
      
      @Override
      public void mouseEntered(MouseEvent e) {}
      
      @Override
      public void mouseClicked(MouseEvent e) {
        String res = show(f, null, 100, 100);
        System.out.println("[" + res + "]");
        
      }
    });
    
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
    f.setVisible(true);
  }
  
  public static String show(Container owner, String value, int xOffset, int yOffset) {
    Frame top = null;
    {
      Container cur = owner;
      while (cur != null) {
        cur = cur.getParent();
        if (cur instanceof Frame) {
          top = (Frame)cur;
          break;
        }
      }
    }
    
    final JDialog dia = new JDialog(top);
    dia.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
    dia.setTitle("Введите новые данные");
    JPanel pane = new JPanel();
    pane.setLayout(new BorderLayout());
    dia.setContentPane(pane);
    
    JScrollPane tascroll = new JScrollPane();
    pane.add(tascroll, BorderLayout.CENTER);
    
    JTextArea ta = new JTextArea();
    ta.setTabSize(4);
    tascroll.setViewportView(ta);
    
    JPanel btns = new JPanel();
    pane.add(btns, BorderLayout.PAGE_END);
    
    JButton ok = new JButton("OK");
    ok.setName("ok");
    btns.add(ok);
    
    JButton cancel = new JButton("Отменить");
    cancel.setName("cancel");
    btns.add(cancel);
    
    final boolean isok[] = new boolean[] { false };
    
    MouseAdapter handler = new MouseAdapter() {
      @Override
      public void mouseClicked(MouseEvent e) {
        isok[0] = "ok".equals(e.getComponent().getName());
        dia.setVisible(false);
      }
    };
    ok.addMouseListener(handler);
    cancel.addMouseListener(handler);
    
    ta.addKeyListener(new KeyAdapter() {
      @Override
      public void keyPressed(KeyEvent e) {
        if (e.isControlDown() && e.getKeyCode() == KeyEvent.VK_ENTER) {
          e.consume();
          isok[0] = true;
          dia.setVisible(false);
          return;
        }
        if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
          e.consume();
          isok[0] = false;
          dia.setVisible(false);
          return;
        }
      }
    });
    
    Point loc = owner.getLocation();
    loc.translate(xOffset, yOffset);
    dia.setLocation(loc);
    dia.setSize(500, 300);
    dia.addWindowListener(new WindowAdapter() {
      @Override
      public void windowClosing(WindowEvent e) {
        dia.setVisible(false);
      }
    });
    ta.setText(value);
    
    dia.setModal(true);
    dia.setVisible(true);
    
    if (!isok[0]) return null;
    return ta.getText();
  }
}
